
from aop.aop_config import load_global_config
from aop.aop_timeit import timeit
from services.base_dev_common_service import BaseDevCommonService


class SyncZAfterAllSynced(BaseDevCommonService):
    @load_global_config('../config/config.json5')
    def __init__(self, model_class=None, time_field=None):
        super().__init__(model_class, time_field)


@timeit(name="SyncZAfterAllSynced")
def update():
    after_all_synced = SyncZAfterAllSynced(model_class=None)
    sql = """
    UPDATE op_order_tb  t1 join  op_order_goods_tb  t2  on t1.tid = t2.tid  and t1.`created` >= DATE_SUB(CURDATE(), INTERVAL 10 DAY) AND t1.`created` < CURDATE()
  SET t2.op_order_tb_id = t1.id;
    """
    res = after_all_synced.execute_native_sql(sql)
    print('update', res)

def select():
    after_all_synced = SyncZAfterAllSynced(model_class=None)
    sql = """
SELECT count(*)
FROM op_order_tb t1
         join op_order_goods_tb t2
              on t2.op_order_tb_id = t1.id and t1.`created` >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND
                 t1.`created` < CURDATE();
      """
    res = after_all_synced.execute_native_sql(sql)
    print('select',res)


def insert():
    after_all_synced = SyncZAfterAllSynced(model_class=None)
    sql = """
    	
INSERT INTO op_pdd_goods_outer_all (
	activity_goods_id,
	goods_name_simple,
	goods_brand,
	goods_category,
	outer_id,
	use_bidd,
	use_statistics,
	create_by,
	create_time,
	update_by,
	update_time,
	deleted,
	spec,
	spec_robot,
	slic_hour,
	plat_form_type,
	wc_goods_name,
	wc_spec,
	sku_abbreviation 
) SELECT
t1.activity_goods_id,
t1.goods_name_simple,
t1.goods_brand,
t1.goods_category,
t1.outer_id,
t1.use_bidd,
t1.use_statistics,
t1.create_by,
t1.create_time,
t1.update_by,
t1.update_time,
t1.deleted,
t1.spec,
t1.spec_robot,
t1.slic_hour,
t1.plat_form_type,
t1.wc_goods_name,
t1.wc_spec,
t1.sku_abbreviation 
FROM
	op_pdd_goods_outer t1
	LEFT JOIN op_pdd_goods_outer_all t2 ON t1.outer_id = t2.outer_id 
	AND t1.plat_form_type = t2.plat_form_type 
WHERE
	t2.outer_id IS NULL 
	AND t1.deleted = 0
    """
    res = after_all_synced.execute_native_sql(sql)
    print('insert', res)


def main():
    # select()
    update()
    insert()

if __name__ == '__main__':
    main()
